// Copyright 2008-2009 The SharpSvn Project
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#include "stdafx.h"

#include "Args/ReintegrationMerge.h"

using namespace SharpSvn::Implementation;
using namespace SharpSvn;
using namespace System::Collections::Generic;

bool SvnClient::ReintegrationMerge(String^ targetPath, SvnTarget^ source)
{
    if (String::IsNullOrEmpty(targetPath))
        throw gcnew ArgumentNullException("targetPath");
    else if (!source)
        throw gcnew ArgumentNullException("source");

    return ReintegrationMerge(targetPath, source, gcnew SvnReintegrationMergeArgs());
}

bool SvnClient::ReintegrationMerge(String^ targetPath, SvnTarget^ source, SvnReintegrationMergeArgs^ args)
{
    if (String::IsNullOrEmpty(targetPath))
        throw gcnew ArgumentNullException("targetPath");
    else if (!source)
        throw gcnew ArgumentNullException("source");
    else if (!args)
        throw gcnew ArgumentNullException("args");
    else if (!IsNotUri(targetPath))
        throw gcnew ArgumentException(SharpSvnStrings::ArgumentMustBeAPathNotAUri, "targetPath");

    EnsureState(SvnContextState::AuthorizationInitialized);
    AprPool pool(%_pool);
    ArgsStore store(this, args, %pool);

#pragma warning(push)
#pragma warning(disable: 4996)
    svn_error_t *r = svn_client_merge_reintegrate(
        source->AllocAsString(%pool),
        source->GetSvnRevision(SvnRevision::Working, SvnRevision::Head)->AllocSvnRevision(%pool),
        pool.AllocDirent(targetPath),
        args->DryRun,
        args->MergeArguments ? AllocArray(args->MergeArguments, %pool) : nullptr,
        CtxHandle,
        pool.Handle);
#pragma warning(pop)

    return args->HandleResult(this, r, targetPath);
}
